    //
//  PlayerTableViewController.m
//  SHEL
//
//  Created by Jennifer Guriel on 11/3/11.
//  Copyright 2011 Washington University. All rights reserved.
//

#import "PlayerTableViewController.h"
#import "PlayerDetailViewController.h"

@implementation PlayerTableViewController


- (id)initWithStyle:(UITableViewStyle)style {
	 // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
	 if (self = [super initWithStyle:style]) {

		 self.title = @"List View";
		 self.tabBarItem.image = [UIImage imageNamed:@"123-id-card.png"];
		 self.navigationItem.title = @"Team SHEL";
		
		 //self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"AppBackground.png"]];
	 }
	 return self;
}
 
-(void) setPlayerData:(NSMutableArray*) data {
	playerData = data;
	[self.tableView reloadData];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	
	if (playerData == nil) {
		playerData = [[NSMutableArray alloc] init];
	}
	if (playerHistory == nil) {
		playerHistory = [[NSMutableArray alloc] init];
	}
	if (playerTrauma == nil) {
		playerTrauma = [[NSMutableArray alloc] init];
	}
	
	UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
    self.navigationItem.backBarButtonItem = backBarButtonItem;
    [backBarButtonItem release];
	
	//[self.navigationController.navigationBar setTintColor:[UIColor orangeColor]];
	
	[self.view setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:@"green.png"]]];
	//self.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.68 blue:0.3 alpha:1.0];
}


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [playerData count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:20.0];
	NSString* lastname = [[playerData objectAtIndex:indexPath.row] objectForKey:@"lastname"];
	NSString* number = [[playerData objectAtIndex:indexPath.row] objectForKey:@"number"];
	cell.textLabel.text = [NSString stringWithFormat:@"#%@ - %@", number, lastname];
	cell.textLabel.textColor = [UIColor whiteColor];
	
	Boolean historyRecordIsToday = FALSE;
	if (playerHistory == nil || [playerHistory count] == 0 || [playerHistory objectAtIndex:0] == nil ||
		[playerHistory count] <= indexPath.row ||
		[playerHistory objectAtIndex:indexPath.row] == nil || [[playerHistory objectAtIndex:indexPath.row] count] == 0 ||
		[[playerHistory objectAtIndex:indexPath.row] objectAtIndex:0] == nil)
	{
		historyRecordIsToday = FALSE;
	}
	else {
		NSString* historyDate = [[[[playerHistory objectAtIndex:indexPath.row] objectAtIndex:0] objectForKey:@"date"] substringToIndex:10];
		//NSLog(@"1) %@", historyDate);
		
		NSDateFormatter *date_formater=[[NSDateFormatter alloc] init];
		[date_formater setDateFormat:@"YYY-MM-dd"];
		NSString* sDate = [date_formater stringFromDate:[NSDate date]];
		[date_formater release];
		//NSLog(@"2) %@",sDate);
		
		if ([historyDate isEqualToString:sDate])
		{
			historyRecordIsToday = TRUE;
			//NSLog(@"match!");
		}
	}
	
	
	// Color of a helmet is based on status value in history
	if (historyRecordIsToday) {
		// Set color based on player status
		if ([[[[playerHistory objectAtIndex:indexPath.row] objectAtIndex:0] objectForKey:@"status"] intValue] == 0) {
			cell.imageView.image = [UIImage imageNamed:@"grey_helmet_sm.png"];
		}
		else if ([[[[playerHistory objectAtIndex:indexPath.row] objectAtIndex:0] objectForKey:@"status"] intValue] == 1) {
			cell.imageView.image = [UIImage imageNamed:@"green_helmet_sm.png"];
		}
		else if ([[[[playerHistory objectAtIndex:indexPath.row] objectAtIndex:0] objectForKey:@"status"] intValue] == 2) {
			cell.imageView.image = [UIImage imageNamed:@"yellow_helmet_sm.png"];
		}
		else {
			cell.imageView.image = [UIImage imageNamed:@"red_helmet_sm.png"];
		}
	}
	else {
		cell.imageView.image = [UIImage imageNamed:@"grey_helmet_sm.png"];
	}

	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	NSString* lastname = [[playerData objectAtIndex:indexPath.row] objectForKey:@"lastname"];
	NSString* number = [[playerData objectAtIndex:indexPath.row] objectForKey:@"number"];
	NSString* nameStr = [NSString stringWithFormat:@"#%@ - %@", number, lastname];
	NSMutableArray* availHistory = nil;
	NSMutableArray* availTrauma = nil;
	if ([playerHistory count] > indexPath.row)
	{
		availHistory = [playerHistory objectAtIndex:indexPath.row];
	}
	if ([playerTrauma count] > indexPath.row)
	{
		availTrauma = [playerTrauma objectAtIndex:indexPath.row];
	}
	PlayerDetailViewController *detailViewController = [[PlayerDetailViewController alloc] initWithNibNameAndIndex:@"PlayerDetailViewController" bundle:nil 
																											 index:indexPath.row 
																											  name:nameStr
																										   history:availHistory
																											trauma:availTrauma];
	[self.navigationController pushViewController:detailViewController animated:YES];
	[detailViewController release];
}

-(void) addPlayerHistory:(int)index history:(NSArray*)history {
	if ([playerHistory count] == 0 || [playerHistory count] < index+1) {
		[playerHistory addObject:history];
	}
	else {
		[playerHistory replaceObjectAtIndex:index withObject:history];
	}
	
	[self.tableView reloadData];
}

-(void) addPlayerTrauma:(int)index trauma:(NSArray*)trauma {
	if ([playerTrauma count] == 0 || [playerTrauma count] < index+1) {
		[playerTrauma addObject:trauma];
	}
	else {
		[playerTrauma replaceObjectAtIndex:index withObject:trauma];
	}
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	[playerData release];
	[playerHistory release];
	[playerTrauma release];
}


@end
